package online.afeibaili.storebackendframework.service.impl

import jakarta.transaction.Transactional
import online.afeibaili.storebackendframework.model.AccountEntity
import online.afeibaili.storebackendframework.model.CouponEntity
import online.afeibaili.storebackendframework.model.ShoppingCartEntity
import online.afeibaili.storebackendframework.model.json.CouponJsonMapper
import online.afeibaili.storebackendframework.repository.AccountRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
import kotlin.jvm.optionals.getOrElse
import kotlin.jvm.optionals.getOrNull


/**
 * 账户实现
 *
 *@author AfeiBaili
 *@version 2025/10/17 15:45
 */

@Service
@Transactional
class AccountServiceImpl {
    @Autowired
    lateinit var repository: AccountRepository

    @Autowired
    lateinit var shoppingCartServiceImpl: ShoppingCartServiceImpl

    @Autowired
    lateinit var couponServiceImpl: CouponServiceImpl

    //创建账户时自动创建购物车
    fun getOrCreate(account: AccountEntity): AccountEntity? {
        return repository.findById(account.id).getOrElse {
            val accountEntity: AccountEntity = repository.save(account)
            shoppingCartServiceImpl.create(ShoppingCartEntity(accountEntity.id, "[]"))
            return accountEntity
        }
    }

    fun getAll(): List<AccountEntity> {
        return repository.findAll()
    }

    fun getByIds(ids: List<String>): List<AccountEntity> {
        return repository.findByIdIn(ids)
    }

    fun addCoupons(accountId: String, coupons: List<CouponJsonMapper>): Boolean {
        val accountEntity: AccountEntity = repository.findById(accountId).getOrElse {
            return false
        }
        val couponsJson = accountEntity.coupons as String?
        val couponJsonMappers = CouponJsonMapper.toListObject(couponsJson ?: "[]")
        coupons.forEach { coupon -> if (couponServiceImpl.getById(coupon.couponId) == null) return false }
        couponJsonMappers.addAll(coupons)

        accountEntity.coupons = CouponJsonMapper.toListJson(couponJsonMappers)
        repository.save(accountEntity)
        return true
    }

    fun updateCoupons(accountId: String, json: String): AccountEntity? {
        val entity: AccountEntity = repository.findById(accountId).getOrElse {
            return null
        }
        entity.coupons = json
        return repository.save(entity)
    }


    fun updateLastLoginTime(accountId: String) {
        val entity: AccountEntity = repository.findById(accountId).get()
        entity.lastLoginTime = System.currentTimeMillis()
        repository.save(entity)
    }

    fun getCoupons(id: String): List<CouponEntity>? {
        val accountEntity: AccountEntity = repository.findById(id).getOrElse {
            return null
        }
        val couponJsonMappers = CouponJsonMapper.toListObject(accountEntity.coupons)
        return couponServiceImpl.getByIds(couponJsonMappers.map { it.couponId })
    }

    fun getCouponsJsonMapper(id: String): List<CouponJsonMapper>? {
        val accountEntity: AccountEntity = repository.findById(id).getOrElse { return null }
        return CouponJsonMapper.toListObject(accountEntity.coupons)
    }

    fun removeCoupons(account: AccountEntity, couponIdsAndCount: List<Pair<Long, Int>>): Boolean {
        val coupons: List<CouponJsonMapper>? = getCouponsJsonMapper(account.id)
        if (coupons == null) return false
//        coupons.filter {  }
        TODO()
    }

    fun remove(id: String): AccountEntity? {
        val accountEntity: AccountEntity? = repository.removeById(id)
        return accountEntity
    }

    fun getById(id: String): AccountEntity? {
        val optional: Optional<AccountEntity> = repository.findById(id)
        return optional.getOrNull()
    }

    fun removeByName(name: String): List<AccountEntity>? {
        val removeByNickname: List<AccountEntity> = repository.removeByNickname(name)
        if (removeByNickname.isEmpty()) return null
        return removeByNickname
    }
}